package xyz.jcat.common.util;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import xyz.jcat.common.exception.ConvertException;

import java.security.Key;
import java.security.KeyPair;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.Base64;

public class RSAUtils {

    private static final String ALGORITHM = "RSA";

    private static final String PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDLnWLTsbd4duo9uGV+kUeMxQcczFyCCcykwv/Wnkhv3eejLNS5HMeb5SbmsqEafpILQNoeUHxQaMqhNxpQqy2/PFB781MnEM7P4SE0hzHenQQll8aG+Adr8m9Qg4Bn6LMZbu1PzBsv/rktQWBs8MwNrL2lz6Rdi8W336y4QL/HOwIDAQAB";
    private static final String PRIVATE_KEY = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAMudYtOxt3h26j24ZX6RR4zFBxzMXIIJzKTC/9aeSG/d56Ms1Lkcx5vlJuayoRp+kgtA2h5QfFBoyqE3GlCrLb88UHvzUycQzs/hITSHMd6dBCWXxob4B2vyb1CDgGfosxlu7U/MGy/+uS1BYGzwzA2svaXPpF2LxbffrLhAv8c7AgMBAAECgYAehZkq4pj+FdVHKz0Lm/t9vosyZe/4Dj/+8rwnD9ML8denPjElDotdcxxBuMUtk4uQ5EfOA4KfCxZU1E29ietxWqlhmUrtVtdr2fcmG5IAgUg6XomQAvsuzYf9D4U9tb/6EWejC/uMbnK2Hqa3mwUyazHpDKM4TvtkFYw5YE9/AQJBAPHXCoSkB2NIkopOhi7mr+dr0LoVUF9H4J4hxWBjJ9IOWW4g0Nt6pyXPIHAixtlIldv8UeKtvLQm+O1e9gQYTT0CQQDXiWJaWDpQ1w4wYvCRM++qhu708DVvmLj3JhNHvq107o2Egb94leRQLk8TMw/y6ARQObZcYhHGbeJcnWwFYx3XAkA3+b8Mfm7bUxPgAVbbijeSyAiy2XYV56wczdJCghDYdMvMSros4tHHVY67hcEiz8Mn/NnzLqVryU16m60zu3A9AkEAm/hl/Ny1roRBV1Bxp5G+0Fb+mq8s7AnHZ8ncz+Ey4lgyfn3p0dgeNMueG1WrJ+swG+H3jhjFpiMkWELuxWM5VwJAem3YL/qsn/G2MG1lFeSWJhhwmZk45fsHhVqNho/6KjGSYyB0RL+P2gj0JPdPt1evn8qDxqk5tNeKkem/4m3KZA==";

    private static final RSA rsa = new RSA(PRIVATE_KEY, PUBLIC_KEY);

    //TODO 写入缓存
    public static String getPublicKey() {
        return PUBLIC_KEY;
    }

    public static RSASimpleKeyPair generateKeyPair() {
        KeyPair rsa = SecureUtil.generateKeyPair(ALGORITHM);
        RSAPrivateKey privateKey = (RSAPrivateKey) rsa.getPrivate();
        RSAPublicKey publicKey = (RSAPublicKey) rsa.getPublic();
        return new RSASimpleKeyPair(getKeyStr(publicKey), getKeyStr(privateKey));
    }

    private static String getKeyStr(Key key) {
        return Base64.getEncoder().encodeToString(key.getEncoded());
    }

    public static String decrypt(String str) {
        return decrypt(str, KeyType.PrivateKey);
    }
    public static String decrypt(String str, KeyType keyType) {
        try {
            return rsa.decryptStr(str, keyType);
        }catch (Exception e) {
            throw new ConvertException("RSA 解码异常");
        }
    }

    public static String encrypt(String str) {
        return encrypt(str, KeyType.PublicKey);
    }

    public static String encrypt(String str, KeyType keyType) {
        return rsa.encryptBase64(str, keyType);
    }
    public static void main(String[] args) {
        String encrypt = encrypt("123456");
        System.out.println(encrypt);
        String decrypt = decrypt(encrypt);
        System.out.println(decrypt);
    }

    @Getter
    @Setter
    @AllArgsConstructor
    public static class RSASimpleKeyPair {
        private String publicKey;
        private String privateKey;
    }
}
